<?php

  class Tool_gd{

    /*
     * fonction pour créer les couleurs des matrices de DTW
     * $_maxint == frontière à ne pas franchir
     */

    static public function matrixDTW2matrix_color($_matrix,$_maxint){
      $array_val=array();
      
      foreach($_matrix as $ligne){
        foreach($ligne as $val){
          if($val!==$_maxint) $array_val[$val]++;
        }
      }
//      ksort($array_val);
//      print_r($array_val);
//      die();
      $min=min(array_keys($array_val));
      $max=max(array_keys($array_val));
//      $min=200;
//      $min=195;
//      $max=230;
      $interval=abs($max-$min);
      $max_color=255;
      foreach($array_val as $val => $mt){
        $pos=abs($val-$min);
/*
        if($val==200) $array_color[$val]=10;
        elseif($val==201) $array_color[$val]=100;
        elseif($val==202) $array_color[$val]=100;
        elseif($val==203) $array_color[$val]=150;
        elseif($val==204) $array_color[$val]=200;
        elseif($val==205) $array_color[$val]=250;
        else $array_color[$val]=255;
*/
        $array_color[$val]=floor(($pos/$interval)*$max_color);
        if($array_color[$val]>255) $array_color[$val]=255;
        if($array_color[$val]<0) $array_color[$val]=0;
      }

      $palette=array_unique($array_color);
      asort($palette);
      $matrix_color=array();
      foreach($_matrix as $y=>$ligne){
        foreach($ligne as $x=>$dist){
          if($dist===$_maxint) $matrix_color[$y][$x]=$_maxint;
          else $matrix_color[$y][$x]=$array_color[$dist];
        }
      }
      return array('matrix'=>$matrix_color,'palette'=>$palette);
    }

    /*
     * fonction qui dessine la matrice de score de la dtw
     * ainsi que les chemins obtenus
     */

    static public function draw_matrixDTW($_dtw,$_array_routes,$_filename){
      $matrix=$_dtw->__get_array_res();
      $wtf=$_dtw->__get_maxint();
      $matrix_color=self::matrixDTW2matrix_color($matrix,$wtf);

      $height=count($matrix_color['matrix']);
      $width=count($matrix_color['matrix'][0]);
      $gd = imagecreatetruecolor($width, $height);

      $white=imagecolorAllocate($gd,255,255,255); 
      $black=imagecolorAllocate($gd,0,0,0); 
      $red=imagecolorAllocate($gd,255,0,0); 
      $blue=imagecolorAllocate($gd,0,255,0); 
//      $green=imagecolorAllocate($gd,0,0,255); 

      $gd_palette=array();

//      print_r($matrix_color['palette']);

      foreach($matrix_color['palette'] as $color){
        $gd_palette[$color]=imageColorAllocate($gd,0,$color,0);
        $gd_count[$color]++;
      }

//      print_r($gd_palette);
//      die();
      krsort($matrix_color);
      foreach($matrix_color['matrix'] as $y=>$ligne){
        krsort($ligne);
        foreach($ligne as $x=>$color){
          if($color===$wtf) imagesetpixel($gd, $x,$y, $white);
          else imageSetPixel($gd,$x,$y,$gd_palette[$color]);
          $gd_count[$color]++;
        }
      }
      foreach($_array_routes as $route){
        foreach($route as $coord){
          imageSetPixel($gd,$coord[1],$coord[0],$red);
        }
      }
//      print_r($gd_count);
//      die();
/* 
 $corners[0] = array('x' => 100, 'y' => 10);  
 $corners[1] = array('x' => 0, 'y' => 190);  
 $corners[2] = array('x' => 200, 'y' => 190);  
  
 $red = imagecolorallocate($gd, 255, 0, 0);  
  
 for ($i = 0; $i < 100; $i++) {  
 imagesetpixel($gd, round($x),round($y), $red);  
 $a = rand(0, 2);  
 $x = ($x + $corners[$a]['x']) / 2;  
 $y = ($y + $corners[$a]['y']) / 2;  
 }  
 */ 
 imagepng($gd,$_filename);  
 imagedestroy($gd);
die();

      $couleur_fond=ImageColorAllocate($img,0,0,0);

//      $palette=$matrix_color['palette'];
//      $palette_gd=array();
//      $palette_gd['wtf']=imageColorAllocate($img,0,0,0);
/*
      foreach($palette as $dist=>$color){
        $palette_gd[$color]=imageColorAllocate($img,0,$color,0);
      }
*/
//      print_r($palette_gd);
//      die();
      foreach($matrix_color['matrix'] as $y=>$ligne){
        foreach($ligne as $x=>$color){

/*
          if($color==array('r'=>255,'g'=>255,'b'=>255)){
            imageSetPixel($img,$x,$y,$palette_gd['wtf']);
          }
*/
/*
          else{
            $g=$color['g'];
            imageSetPixel($img,$x,$y,$palette_gd[$g]);
          }
*/
        }
      }

      ImagePng($img,$_filename);
    }

  }

?>
